﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;


namespace EntGuayasSLA.BL
{
    public static class MyMail
    {
        /// <summary>
        /// Permite enviar un mail a traves de un servidor smtp
        /// </summary>
        /// <param name="strMailFrom">Desde</param>
        /// <param name="strMailTo">Hasta</param>
        /// <param name="strAsunto">Asunto</param>
        /// <param name="strMensajeHTML">Mensaje Html</param>
        /// <returns></returns>
        public static bool Enviar(string strMailFrom, string strMailTo, string strAsunto, string strMensajeHTML, Boolean isBodyHtml,string prmInv)
        {
            bool booEstadoMail = false;
            //try
            //{

            MailMessage mmCorreo = new MailMessage();
            MailAddressCollection to = new MailAddressCollection();

            if (strMailFrom == "")
            {
                strMailFrom = ConfigurationManager.AppSettings.Get("MailRemite");
            }
            mmCorreo.From = new MailAddress(strMailFrom);
            mmCorreo.To.Add(new MailAddress(strMailTo));
            mmCorreo.Subject = strAsunto;
            mmCorreo.Body = strMensajeHTML;
            mmCorreo.BodyEncoding = Encoding.UTF8;
            mmCorreo.IsBodyHtml = isBodyHtml;
            /*
            try
            {
                var smtp = new SmtpClient(ConfigurationManager.AppSettings.Get("SMTP"));
                smtp.Send(mmCorreo);

            }
            catch 
            {*/
            //NetworkCredential
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings.Get("MailRemite"), ConfigurationManager.AppSettings.Get("KeyMail"));
            client.Port = int.Parse(ConfigurationManager.AppSettings.Get("PuertoMail"));
            client.Host = ConfigurationManager.AppSettings.Get("ServidorMail");
            if (ConfigurationManager.AppSettings.Get("SSLMail") == "SI")
                client.EnableSsl = true; //Esto es para que vaya a través de SSL que es obligatorio con GMail
            
            try
            {
                client.Send(mmCorreo);
            }
            catch (System.Net.Mail.SmtpException e7)
            {
                booEstadoMail = false;
                throw new Exception(prmInv+"::"+e7.Message);
            }

            //}
            booEstadoMail = true;

            //}
            //catch (Exception e8)
            //{
            //    throw new Exception(e8.Message);
            //}
            return booEstadoMail;
        }
    }
}
